﻿
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.ComponentModel;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
//using System.Windows.Controls;
//using System.Windows.Data;
//using System.Windows.Documents;
using System.Windows.Input;
//using System.Windows.Media;
//using System.Windows.Media.Imaging;
//using System.Windows.Navigation;
//using System.Windows.Shapes;
using System.Net.NetworkInformation;
using Timer = System.Timers.Timer;
using System.Collections.Concurrent;
using System.Data;
using System.Globalization;
using System.Text.RegularExpressions;
using bwtSocket;
using static AutoTestCommunication_Service.Public;
using AutoTestCommunication_Service.Class;
using System.Runtime.InteropServices;
using System.Timers;
using System.ServiceProcess;
using NPOI.SS.Formula.Functions;


namespace AutoTestCommunication_Service
{

    public class DigitalService
    {
        //===========================================================================================================================================================


        private BWTTcpServer tcpServer;
        private Timer TcpconnectTimer = new Timer();
        private Timer mysqlguangxianTimer = new Timer();
        private Timer mysqlbengyuanTimer = new Timer();
        private Timer jiandaoyunTimer = new Timer();
        private Timer clientconnect = new Timer();

        public bool ServerOnline = true;
        public bool IsTcpConnectTesting = false;
        public double PixelCoefficient;
        public static List<byte> PackedData { get; set; }
        public string CurrentIP = "";
        //public static BindingLB BDHostHoststate = new BindingLB();
        //public static BindingLB BDopticalfiberstate = new BindingLB();
        //public static BindingLB BDPumpsourcestate = new BindingLB();
        //public static BindingLB BDjiandaoyunstate = new BindingLB();
        //public static BindingLB BDmessage = new BindingLB();

        //public static BindingLB BDstarttime = new BindingLB();
        //public static BindingLB BDendtime = new BindingLB();
        //public static BindingLB BDcosttime = new BindingLB();

        public static ConcurrentDictionary<int, double> ListWSAVE = new ConcurrentDictionary<int, double>();
        public static ConcurrentDictionary<string, string> Command_POLO = new ConcurrentDictionary<string, string>();
        public void MMMM()
        {
            DataPreparation();
            TimerSetting();
        }
        private void DataPreparation()
        {
            try
            {
                Raman.DataPreparation();
                Raman.TimerSetting();

                SQL.Mysqlreadtablestructure3000();

                for (int i = 0; i < ListCommandName.Count; i++)
                {
                    Command_POLO.TryAdd(ListCommandName[i], ListPOLOClass[i]);
                }

                string filePath = Public.Apppath + "txt\\" + "SDT.xls";
                Public.Readfile(filePath, out Public.SDTIBook);
                Public.SDT = Public.ReadQueryTable(true, Public.SDTIBook);

                filePath = Public.Apppath + "txt\\" + "SDT1.xls";
                Public.Readfile(filePath, out Public.SDT1IBook);
                Public.SDT1 = Public.ReadQueryTable(true, Public.SDT1IBook);

                filePath = Public.Apppath + "txt\\" + "SDT2.xls";
                Public.Readfile(filePath, out Public.SDT2IBook);
                Public.SDT2 = Public.ReadQueryTable(true, Public.SDT2IBook);

                filePath = Public.Apppath + "txt\\" + "WCON.xls";
                Public.Readfile(filePath, out Public.WCONIBook);
                Public.WCON = Public.ReadQueryTable(true, Public.WCONIBook);
                //for (int i = 115; i < 119; i++)
                //{
                //    Public.PCB.TryAdd(i.ToString(), "P175");
                //}
                //for (int i = 135; i < 139; i++)
                //{
                //    Public.PCB.TryAdd(i.ToString(), "P175");
                //}
                //for (int i = 145; i < 149; i++)
                //{
                //    Public.PCB.TryAdd(i.ToString(), "P175");
                //}
                //for (int i = 175; i < 179; i++)
                //{
                //    Public.PCB.TryAdd(i.ToString(), "P175");
                //}

                for (int i = 205; i < 210; i++)
                {
                    Public.PCB.TryAdd(i.ToString(), "P205");
                }
                for (int i = 215; i < 220; i++)
                {
                    Public.PCB.TryAdd(i.ToString(), "P205");
                }
                for (int i = 225; i < 230; i++)
                {
                    Public.PCB.TryAdd(i.ToString(), "P225");
                }
                for (int i = 235; i < 240; i++)
                {
                    Public.PCB.TryAdd(i.ToString(), "P235");
                }
                for (int i = 245; i < 250; i++)
                {
                    Public.PCB.TryAdd(i.ToString(), "P245");
                }
                for (int i = 405; i < 410; i++)
                {
                    Public.PCB.TryAdd(i.ToString(), "P405");
                }
                Public.PCB.TryAdd("352", "P352");
                Public.PCB.TryAdd("506", "P405");
                Public.PCB.TryAdd("303", "P303");
                Public.BDParameter();
                Public.RLPCD.TryAdd("20", "8");
                Public.RLPCD.TryAdd("40", "80");
                Public.RLPCD.TryAdd("50", "100");
                Public.RLPCD.TryAdd("100", "38");
                Public.RLPCD.TryAdd("260", "100");
                Public.RLPCD.TryAdd("400", "100");

                ListWSAVE.TryAdd(0, 0);
                ListWSAVE.TryAdd(1, 5);
                ListWSAVE.TryAdd(2, 10);
                ListWSAVE.TryAdd(3, 15);
                ListWSAVE.TryAdd(4, 20);
                ListWSAVE.TryAdd(5, 30);
                ListWSAVE.TryAdd(6, 40);
                ListWSAVE.TryAdd(7, 50);
                ListWSAVE.TryAdd(8, 60);
                ListWSAVE.TryAdd(9, 70);
                ListWSAVE.TryAdd(10, 80);
                ListWSAVE.TryAdd(11, 85);
                ListWSAVE.TryAdd(12, 90);
                ListWSAVE.TryAdd(13, 95);
                ListWSAVE.TryAdd(14, 100);

                Public.CD001文件号.TryAdd("P405", 0);
                Public.CD001文件号.TryAdd("P205", 1);
                Public.CD001文件号.TryAdd("P225", 2);
                Public.CD001文件号.TryAdd("P235", 3);
                Public.CD001文件号.TryAdd("P245", 4);
                Public.CD001文件号.TryAdd("P303", 5);
                Public.CD001文件号.TryAdd("P352", 6);

                Public.PowerLetter.TryAdd("D", "D");
                Public.PowerLetter.TryAdd("P", "D");
                Public.PowerLetter.TryAdd("F", "D");
                Public.PowerLetter.TryAdd("G", "D");

                Public.PowerLetter.TryAdd("T", "T");
                Public.PowerLetter.TryAdd("S", "T");

                if (tcpServer == null) BWTTcpServer.pushSockets = new PushSockets(Rev);

                tcpServer = new BWTTcpServer();

                StartServerToolStripMenuItem();

                //string WCON = Public.SeekWCON("BFL-CW2000T-3-W-N");
            }
            catch (Exception ex)
            {
                TraceMethodInfo(ex.Message + ex.StackTrace);
            }

        }
        private void TimerSetting()
        {
           
            //多线程，先计时等一秒
            //TcpconnectTimer.AutoReset = true;
            //TcpconnectTimer.Interval = 2000;
            //TcpconnectTimer.Enabled = true;
            //TcpconnectTimer.Elapsed += TcpConnectEvent;

            //mysqlguangxianTimer.AutoReset = true;
            //mysqlguangxianTimer.Interval = 2000;        
            //mysqlguangxianTimer.Elapsed += mysqlguangxianTimerEvent;
            //mysqlguangxianTimer.Enabled = true;

            //mysqlbengyuanTimer.AutoReset = true;
            //mysqlbengyuanTimer.Interval = 2000;
            //mysqlbengyuanTimer.Elapsed += mysqlbengyuanTimerEvent;
            //mysqlbengyuanTimer.Enabled = true;

            //jiandaoyunTimer.AutoReset = true;
            //jiandaoyunTimer.Interval = 2000;
            //jiandaoyunTimer.Elapsed += jiandaoyunTimeEvent;
            //jiandaoyunTimer.Enabled = true;


            //System.Timers.Timer aTimer = new System.Timers.Timer(); //初始化定时器
            //aTimer.Interval = 60000;//配置时间1分钟
            //aTimer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimedEvent);
            //aTimer.AutoReset = true;//每到指定时间Elapsed事件是到时间就触发
            //aTimer.Enabled = true; //指示 Timer 是否应引发 Elapsed 事件。
        }
        //定时器触发的处理事件

        private void StartServerToolStripMenuItem()
        {
            try
            {
                if (BDHostHoststate.Startlisten == false)
                {
                    tcpServer.InitSocket(IPAddress.Any, 8088);
                    tcpServer.Start();
                    BDHostHoststate.Startlisten = true;
                    //string message = "服务器开启！";
                    //TraceMethodInfo(message);
                }
            }
            catch (Exception ex)
            {
                TraceMethodInfo(ex.Message + ex.StackTrace);
            }
        }

        /// <summary>
        /// 停止服务监听
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StopServerToolStripMenuItem()
        {
            tcpServer.Stop();
        }

        /// <summary>
        /// 处理接收到客户端的请求和数据
        /// </summary>
        /// <param name="sks"></param>

        private void Rev(bwtSocket.Sockets sks)
        {
            try
            {
                if (sks.ex != null)
                {
                    if (sks.ClientDispose)
                    {

                        //断线获得事件
                        string message = sks.Ip.ToString() + "下线1";
                        //TraceMethodInfo(message);         
                        CreatLog(sks.Ip.Address.ToString(), message);
                        //listBoxServerInfo.Items.Add(string.Format("{0}客户端：{1}下线！", DateTime.Now.ToString(), sks.Ip));
                        //if (treeViewClientList.Nodes[0].Nodes.ContainsKey(sks.Ip.ToString()))
                        //{
                        //    if (DicTreeIPEndPoint.Count != 0)
                        //    {
                        //        removTreeIPEndPoint(sks.Ip);
                        //        treeViewClientList.Nodes[0].Nodes.RemoveByKey(sks.Ip.ToString());
                        //        toolStripStatusLabelClientNum.Text = (int.Parse(toolStripStatusLabelClientNum.Text) - 1).ToString();//treeViewClientList.Nodes[0].Nodes.Count.ToString();
                        //    }
                        //}
                    }
                }
                else
                {
                    if (sks.NewClientFlag)
                    {
                        Public.AddNew3000EP(sks.Ip.ToString());
                        string message = sks.Ip.ToString() + "连接成功";
                        //TraceMethodInfo(message);
                        CreatLog(sks.Ip.Address.ToString(), message);
                        //listBoxServerInfo.Items.Add(string.Format("{0}新的客户端：{0}链接合格", DateTime.Now.ToString(), sks.Ip));
                        //TreeNode tn = new TreeNode();
                    }
                    else if (sks.Offset == 0)
                    {
                        string message = sks.Ip.ToString() + "下线2";
                        //TraceMethodInfo(message);
                        CreatLog(sks.Ip.Address.ToString(), message);
                        //listBoxServerInfo.Items.Add(string.Format("{0}客户端:{1}下线.!", DateTime.Now.ToString(), sks.Ip));
                    }
                    else
                    {
                        //SendReadData(sks);

                        byte[] buffer = new byte[sks.Offset];
                        Array.Copy(sks.RecBuffer, buffer, sks.Offset);

                        if (buffer[0] == 235 & buffer[1] == 144)
                        {
                            int bufferContentLength = 0;
                            for (int i = 0; i < 4; i++)//计算出数据长度
                            {
                                bufferContentLength = (int)(bufferContentLength + Convert.ToInt32(buffer[i + 2] * Math.Pow(255, i)));
                            }

                            if (bufferContentLength == buffer.Length - 8)//数据长度和收到的一致，开始解析数据
                            {
                                int contentLength = bufferContentLength - 4;//CRC+/R/N
                                byte[] content = new byte[contentLength];
                                Array.Copy(buffer, 8, content, 0, contentLength);

                                int CRCcontentLength = bufferContentLength + 2;
                                byte[] CRCcontent = new byte[CRCcontentLength];
                                Array.Copy(buffer, 6, CRCcontent, 0, CRCcontent.Length);

                                if (Public.CRCCheck(CRCcontent))
                                {
                                    //校验合格开始解析数据
                                    string strcontent = Encoding.UTF8.GetString(content);

                                    string[] strRev = strcontent.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                                    //Public.Logger.Info("received" + strcontent);
                                    CreatLog(sks.Ip.Address.ToString(), "received" + strcontent);
                                    if (EP.ContainsKey(sks.Ip.ToString()))
                                    {
                                        if (strRev[2] == "ST")
                                        {
                                            //最后一个功率记录更新PD
                                            string CommandName = strRev[2] + strRev[3];
                                            if (ListCommandName.Contains(CommandName))
                                            {
                                                EP[sks.Ip.ToString()].Last_POLO = Command_POLO[CommandName];
                                            }
                                            //
                                            if (strRev[3] == "99")//下位通信结束开始上传
                                            {
                                                tcpServer.SendToClient(sks.Ip, PackData("OK"));
                                                string message;
                                                if (EP[sks.Ip.ToString()].CDSDT[2].Stringvalue == "N/A" || EP[sks.Ip.ToString()].CDSDT[2].Stringvalue == "")
                                                {

                                                    message = sks.Ip.ToString() + EP[sks.Ip.ToString()].CDSDT[2].Stringvalue + "测试未完成结束";
                                                }
                                                else
                                                {
                                                    EP[sks.Ip.ToString()].CDSDT[90].Stringvalue = "测试完成";
                                                    DateTime DT = Public.ChinaAreaTime(DateTime.Now);
                                                    //记录结束时间
                                                    string datetime = DT.AddHours(-0).ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.CurrentCulture);
                                                    EP[sks.Ip.ToString()].CDSDT[91].Stringvalue = datetime;

                                                    message = sks.Ip.ToString() + EP[sks.Ip.ToString()].CDSDT[2].Stringvalue + "测试完成结束";
                                                    MysqlUpdate(sks.Ip.ToString());
                                                    //EP[sks.Ip.ToString()].MysqlCanUpdate = true;
                                                }

                                                for (int i = 0; i < EP[sks.Ip.ToString()].CDSDT.Count; i++)
                                                {
                                                    if (EP[sks.Ip.ToString()].CDSDT[i].Local_NameE != "JobNo")
                                                    {
                                                        EP[sks.Ip.ToString()].CDSDT[i].Stringvalue = "N/A";
                                                    }
                                                }
                                                //for (int i = 0; i < 4; i++)
                                                //{
                                                //    EP[sks.Ip.ToString()].Io[i] = 0;
                                                //    EP[sks.Ip.ToString()].Ith[i] = 0;
                                                //    EP[sks.Ip.ToString()].Po[i] = 0;
                                                //}
                                                //message = sks.Ip.ToString() + EP[sks.Ip.ToString()].CDSDT[2].Stringvalue + "测试未完成结束";
                                                CreatLog(sks.Ip.Address.ToString(), message);
                                                //TraceMethodInfo(message);
                                            }
                                            //else if (strRev[3] == "101")// 温度报警
                                            //{
                                            //    string TestTime = EP[sks.Ip.ToString()].CDSDT[0].Stringvalue;
                                            //    string SN = EP[sks.Ip.ToString()].CDSDT[2].Stringvalue;
                                            //    string DeviceIP = sks.Ip.Address.ToString();
                                            //    //SQL.Mysqlcameraalarm(TestTime, SN, DeviceIP);
                                            //}
                                            else if (strRev[3] == "3" || strRev[3] == "14")// PD分组
                                            {

                                                for (int i = 0; i < EP[sks.Ip.ToString()].CDSDT.Count; i++)
                                                {
                                                    string CommandName1 = strRev[2] + strRev[3];
                                                    string CommandName2 = EP[sks.Ip.ToString()].CDSDT[i].Local_CommandName;

                                                    if (CommandName1 == CommandName2)
                                                    {
                                                        if (strRev.Length == 7)//ST3
                                                        {
                                                            //EP[sks.Ip.ToString()].CDSDT[i].Stringvalue = strRev[4];
                                                            //EP[sks.Ip.ToString()].CDSDT[i + 1].Stringvalue = "N/A";
                                                            //EP[sks.Ip.ToString()].CDSDT[i + 2].Stringvalue = "N/A";
                                                            //EP[sks.Ip.ToString()].CDSDT[i + 3].Stringvalue = strRev[5];
                                                            //EP[sks.Ip.ToString()].CDSDT[i + 4].Stringvalue = strRev[6];
                                                            EP[sks.Ip.ToString()].CDSDT[i].Stringvalue = "N/A";
                                                            EP[sks.Ip.ToString()].CDSDT[i + 1].Stringvalue = "N/A";
                                                            EP[sks.Ip.ToString()].CDSDT[i + 2].Stringvalue = strRev[4];
                                                            EP[sks.Ip.ToString()].CDSDT[i + 3].Stringvalue = strRev[5];
                                                            EP[sks.Ip.ToString()].CDSDT[i + 4].Stringvalue = strRev[6];
                                                        }
                                                        else if (strRev.Length == 8)//1101
                                                        {
                                                            EP[sks.Ip.ToString()].CDSDT[i].Stringvalue = strRev[4];
                                                            EP[sks.Ip.ToString()].CDSDT[i + 1].Stringvalue = strRev[5];
                                                            EP[sks.Ip.ToString()].CDSDT[i + 2].Stringvalue = "N/A";
                                                            EP[sks.Ip.ToString()].CDSDT[i + 3].Stringvalue = strRev[6];
                                                            EP[sks.Ip.ToString()].CDSDT[i + 4].Stringvalue = strRev[7];

                                                        }
                                                        else if (strRev.Length == 9)//ST14
                                                        {
                                                            EP[sks.Ip.ToString()].CDSDT[i].Stringvalue = "N/A";
                                                            EP[sks.Ip.ToString()].CDSDT[i + 1].Stringvalue = "N/A";
                                                            EP[sks.Ip.ToString()].CDSDT[i + 2].Stringvalue = strRev[4];
                                                            EP[sks.Ip.ToString()].CDSDT[i + 3].Stringvalue = strRev[5];
                                                            EP[sks.Ip.ToString()].CDSDT[i + 4].Stringvalue = strRev[6];
                                                            EP[sks.Ip.ToString()].CDSDT[i + 14].Stringvalue = strRev[7];
                                                            EP[sks.Ip.ToString()].CDSDT[i + 15].Stringvalue = strRev[8];
                                                        }
                                                        else
                                                        {
                                                            EP[sks.Ip.ToString()].CDSDT[i].Stringvalue = "N/A";
                                                            EP[sks.Ip.ToString()].CDSDT[i + 1].Stringvalue = "N/A";
                                                            EP[sks.Ip.ToString()].CDSDT[i + 2].Stringvalue = "N/A";
                                                            EP[sks.Ip.ToString()].CDSDT[i + 3].Stringvalue = "N/A";
                                                            EP[sks.Ip.ToString()].CDSDT[i + 4].Stringvalue = "N/A";

                                                        }
                                                        break;
                                                    }
                                                }
                                                //EP[sks.Ip.ToString()].MysqlCanUpdate = true;
                                                MysqlUpdate(sks.Ip.ToString());
                                                tcpServer.SendToClient(sks.Ip, PackData("OK"));
                                            }
                                            else if (strRev[3] == "121" || strRev[3] == "122" || strRev[3] == "123" || strRev[3] == "124" || strRev[3] == "125" || strRev[3] == "126")
                                            {
                                                tcpServer.SendToClient(sks.Ip, PackData("OK"));
                                            }
                                            else if (strRev[3] == "127" || strRev[3] == "128" || strRev[3] == "129" || strRev[3] == "130" || strRev[3] == "131" || strRev[3] == "132")
                                            {
                                                tcpServer.SendToClient(sks.Ip, PackData("OK"));
                                            }
                                            else
                                            {
                                                if (strRev[3] == "1")
                                                {
                                                    string SN = strRev[1];
                                                    //SN = "FMNG017359";
                                                    string PN;
                                                    string PRTI; //功率电阻型号指令
                                                    string RLP; //红光功率
                                                    string PCBSN;//PCB版号
                                                    string bengyuanSN;//泵源SN
                                                    string PowerPN; //电源PN
                                                    string NPRP = "2";//功率电阻并联个数
                                                    BDstarttime.LBContent = "";
                                                    BDendtime.LBContent = "";
                                                    BDcosttime.LBContent = "";
                                                    //string OldSN = EP[sks.Ip.ToString()].CDSDT[2].Stringvalue;
                                                    string IP = sks.Ip.ToString();
                                                    BDstarttime.LBContent = Public.ChinaAreaTime(DateTime.Now).ToString();
                                                    string message = SN + "开始检测";
                                                    //TraceMethodInfo(message);
                                                    CreatLog(sks.Ip.Address.ToString(), message);
                                                    string IRAYIP = sks.Ip.Address.ToString();
                                                    //if (IRAYIP == "192.168.29.23")
                                                    //{
                                                    //    //Thread thread = new Thread(new ParameterizedThreadStart(IRAYset));
                                                    //    //thread.IsBackground = true;
                                                    //    //thread.Start(IRAYIP);
                                                    //    if(IRAYset(IRAYIP))
                                                    //    {
                                                    //         message = IP + " " + "红外相机设置完成！";
                                                    //        TraceMethodInfo(message);
                                                    //    }
                                                    //    else
                                                    //    {
                                                    //         message = IP + " " + "红外相机设置不成功！";
                                                    //        TraceMethodInfo(message);
                                                    //        return;
                                                    //    }
                                                    //    //IRAYset(IRAYIP);
                                                    //}
                                                    var (data0, data1) = jiandaoyun.GetInformation(SN);
                                                    if (data0 != null)//查询数据是否有效
                                                    {
                                                        EP[sks.Ip.ToString()].DriverType = data1.driver_panel_type;
                                                        EP[sks.Ip.ToString()].PN = data1.SUPER_PRODUCT_TYPE;

                                                        decimal.TryParse(data0.power1, out decimal power1);
                                                        data0.power1 = power1.ToString();
                                                        decimal.TryParse(data0.power2, out decimal power2);
                                                        data0.power2 = power2.ToString();
                                                        decimal.TryParse(data0.power3, out decimal power3);
                                                        data0.power3 = power3.ToString();
                                                        decimal.TryParse(data0.power4, out decimal power4);
                                                        data0.power4 = power4.ToString();

                                                        EP[sks.Ip.ToString()].POWERALL = (double)(power1 + power2 + power3 + power4);
                                                        double powerall = EP[sks.Ip.ToString()].POWERALL;
                                                        if (InitializeData(SN, IP, NPRP, data0, data1, powerall))
                                                        {
                                                            message = SN + ":" + "初始化完成！" + EP[IP].CDSDT[0].Stringvalue;
                                                            //TraceMethodInfo(message);
                                                            CreatLog(sks.Ip.Address.ToString(), message);
                                                        }
                                                        else
                                                        {
                                                            return;
                                                        }
                                                    }
                                                    else
                                                    {
                                                        message = SN + "MES没有此SN的相关信息！";
                                                        //TraceMethodInfo(message);
                                                        CreatLog(sks.Ip.Address.ToString(), message);
                                                        return;
                                                    }

                                                }
                                                for (int i = 0; i < EP[sks.Ip.ToString()].CDSDT.Count; i++)
                                                {
                                                    string CommandName1 = strRev[2] + strRev[3];
                                                    string CommandName2 = EP[sks.Ip.ToString()].CDSDT[i].Local_CommandName;
                                                    if (CommandName1 == CommandName2)
                                                    {
                                                        //ST参数序号不同，前四位0-SN,1-XXX,2-ST,3-XX
                                                        int Index = EP[sks.Ip.ToString()].CDSDT[i].Local_CMDID + 4;
                                                        //判断有没有系数
                                                        if (EP[sks.Ip.ToString()].CDSDT[i].Local_Ratio == 0)
                                                        {
                                                            EP[sks.Ip.ToString()].CDSDT[i].Stringvalue = strRev[Index];
                                                        }
                                                        else
                                                        {
                                                            double DB = Convert.ToDouble(strRev[Index]) / Convert.ToDouble(EP[sks.Ip.ToString()].CDSDT[i].Local_Ratio);
                                                            EP[sks.Ip.ToString()].CDSDT[i].Stringvalue = DB.ToString("00.0");
                                                        }
                                                        //EP[sks.Ip.ToString()].CDSDT[i].doublevalue = Convert.ToDouble(strRev[Index]); 
                                                    }
                                                }

                                                //string message1 = sks.Ip.ToString() + EP[sks.Ip.ToString()].CDSDT[2].Stringvalue + CommandName + "OK";
                                                //TraceMethodInfo(message1);
                                                //CreatLog(sks.Ip.Address.ToString(), message1);
                                                if (strRev[3] == "18")
                                                {
                                                    string SN = strRev[1]; string IP = sks.Ip.ToString();
                                                    //SN = "FMNG018362";
                                                    //string power = EP[sks.Ip.ToString()].PN.Substring(6, 2);

                                                    SQL.MysqlInitializationRead3000(IP, SN);//先检查后插入新数据
                                                    SQL.MysqlInitializationInsert3000(IP);//插入新数据

                                                    string message = SN + "新建数据完成";
                                                    //TraceMethodInfo(message);
                                                    CreatLog(sks.Ip.Address.ToString(), message);
                                                }
                                                else
                                                {
                                                    if (strRev[3] != "1")
                                                    {
                                                        MysqlUpdate(sks.Ip.ToString());
                                                    }

                                                    //EP[sks.Ip.ToString()].MysqlCanUpdate = true;
                                                }
                                                tcpServer.SendToClient(sks.Ip, PackData("OK"));
                                            }
                                        }
                                        else if (strRev[2] == "OK")
                                        {
                                            if (EP[sks.Ip.ToString()].REQstate == true)
                                            {
                                                string strsend;
                                                int SQC = EP[sks.Ip.ToString()].REQsequence + 0;
                                                int REQCount = EP[sks.Ip.ToString()].REQList.Count;
                                                if (SQC == REQCount)
                                                {
                                                    EP[sks.Ip.ToString()].REQstate = false;
                                                    for (int i = 0; i < EP[sks.Ip.ToString()].CDSDT.Count; i++)
                                                    {
                                                        if (EP[sks.Ip.ToString()].CDSDT[i].Local_CommandName == "REQ")
                                                        {
                                                            EP[sks.Ip.ToString()].CDSDT[i].Stringvalue = EP[sks.Ip.ToString()].REQsequence.ToString();
                                                        }
                                                    }
                                                    string message = EP[sks.Ip.ToString()].CDSDT[2].Stringvalue + "REQ完成";
                                                    //TraceMethodInfo(message);
                                                    CreatLog(sks.Ip.Address.ToString(), message);
                                                    strsend = "REQ" + " " + "QUIT";
                                                }
                                                else
                                                {
                                                    strsend = "REQ" + " " + EP[sks.Ip.ToString()].REQList[EP[sks.Ip.ToString()].REQsequence].Strvalue;

                                                    EP[sks.Ip.ToString()].REQsequence = EP[sks.Ip.ToString()].REQsequence + 1;
                                                    for (int i = 0; i < EP[sks.Ip.ToString()].CDSDT.Count; i++)
                                                    {
                                                        if (EP[sks.Ip.ToString()].CDSDT[i].Local_CommandName == "REQ")
                                                        {
                                                            EP[sks.Ip.ToString()].CDSDT[i].Stringvalue = EP[sks.Ip.ToString()].REQsequence.ToString();//REQ序列号
                                                        }
                                                    }
                                                }
                                                tcpServer.SendToClient(sks.Ip, PackData(strsend));
                                            }
                                        }
                                        else if (strRev[2] == "REQ1")
                                        {
                                            string strsend;
                                            EP[sks.Ip.ToString()].REQstate = true;
                                            EP[sks.Ip.ToString()].REQsequence = 1;
                                            strsend = "REQ" + " " + EP[sks.Ip.ToString()].REQList[0].Strvalue;//正式
                                            string message = EP[sks.Ip.ToString()].CDSDT[2].Stringvalue + "REQ开始";
                                            //TraceMethodInfo(message);
                                            CreatLog(sks.Ip.Address.ToString(), message);
                                            for (int i = 0; i < EP[sks.Ip.ToString()].CDSDT.Count; i++)
                                            {
                                                if (EP[sks.Ip.ToString()].CDSDT[i].Local_CommandName == "REQ")
                                                {
                                                    EP[sks.Ip.ToString()].CDSDT[i].Stringvalue = EP[sks.Ip.ToString()].REQsequence.ToString();
                                                }
                                            }
                                            tcpServer.SendToClient(sks.Ip, PackData(strsend));
                                        }
                                        else if (strRev[2].Substring(0, 1) == "W" & strRev[2].Substring(1, 1) == "S")
                                        {
                                            int num = 0;
                                            if (strRev[2].Length == 6)
                                            {
                                                num = Convert.ToInt32(strRev[2].Substring(5, 1));
                                            }
                                            else if (strRev[2].Length == 7)
                                            {
                                                num = Convert.ToInt32(strRev[2].Substring(5, 2));
                                            }

                                            string IP = sks.Ip.ToString();
                                            string ROPOL = "";
                                            ReadData("ROPOL", out ROPOL, IP);
                                            double 最大功率值 = Convert.ToDouble(ROPOL);
                                            double 百分数 = ListWSAVE[num - 1];
                                            int 功率值 = (int)(百分数 * 最大功率值 / 100);
                                            string strsend = "WSAVE" + " " + 百分数.ToString() + " " + 功率值.ToString();
                                            tcpServer.SendToClient(sks.Ip, PackData(strsend));
                                        }
                                        else if (strRev[2] == "RLAMAN")
                                        {
                                            //string strsend = "RLAMAN" + " " + "880";
                                            //tcpServer.SendToClient(sks.Ip, PackData(strsend));
                                            Task.Run(() => { RLAMAN(sks.Ip); });
                                        }
                                        else if (strRev[2] == "RLAMANS")
                                        {
                                            //string strsend = "RLAMANS" + " " + "1";
                                            //tcpServer.SendToClient(sks.Ip, PackData(strsend));
                                            Task.Run(() => { RLAMANS(sks.Ip); });
                                        }
                                        else if (strRev[2] == "GETCURRENT")
                                        {
                                            string strsend = "GETCURRENT" + " " + EP[sks.Ip.ToString()].I2800 + " " + EP[sks.Ip.ToString()].I3000;
                                            tcpServer.SendToClient(sks.Ip, PackData(strsend));
                                        }
                                        else if (strRev[2] == "BOARDID")
                                        {
                                            string strsend = "BOARDID" + " " + EP[sks.Ip.ToString()].PCBSN;
                                            tcpServer.SendToClient(sks.Ip, PackData(strsend));
                                        }
                                        else if (strRev[2] == "WPRV")
                                        {
                                            string strsend = "WPRV" + " " + "25";
                                            tcpServer.SendToClient(sks.Ip, PackData(strsend));
                                        }
                                        else if (strRev[2] == "WPRNUM")
                                        {
                                            string strsend = "WPRNUM" + " " + "2";
                                            tcpServer.SendToClient(sks.Ip, PackData(strsend));
                                        }
                                        else if (strRev[2] == "WPN")
                                        {
                                            string powerall = "";
                                            string analog = "";//初始模拟量
                                            string auplimit = "";//电流上限
                                            if (EP[sks.Ip.ToString()].POWERALL < 4325 && EP[sks.Ip.ToString()].POWERALL >= 4200)
                                            {
                                                powerall = "3000";
                                                analog = "1500";
                                                auplimit = "48000";
                                            }
                                            else if (EP[sks.Ip.ToString()].POWERALL >= 4325)
                                            {
                                                powerall = "3300";
                                                analog = "1800";
                                                auplimit = "48000";
                                            }
                                            else
                                            {
                                                powerall = "0";
                                                analog = "0";
                                                auplimit = "0";
                                            }
                                            string strsend = "WPN" + " " + EP[sks.Ip.ToString()].PN + " "+ EP[sks.Ip.ToString()].DriverType + " " + powerall + " " + analog + " " + auplimit;
                                            tcpServer.SendToClient(sks.Ip, PackData(strsend));
                                        }
                                        else
                                        {
                                            string strsend;
                                            string conmmandName = strRev[2];
                                            strsend = conmmandName;

                                            for (int i = 0; i < EP[sks.Ip.ToString()].CDSDT.Count; i++)
                                            {
                                                if (EP[sks.Ip.ToString()].CDSDT[i].Local_CommandName == conmmandName)
                                                {

                                                    if (EP[sks.Ip.ToString()].CDSDT[i].Local_Ratio == 0)
                                                    {
                                                        strsend = strsend + " " + EP[sks.Ip.ToString()].CDSDT[i].Stringvalue;
                                                    }
                                                    else
                                                    {
                                                        Public.Parameter Pa = EP[sks.Ip.ToString()].CDSDT[i];
                                                        int PointLong = Pa.Local_Ratio.ToString().Length - 1;
                                                        double RatioValue = Math.Round(Convert.ToDouble(Pa.Stringvalue), PointLong) * Pa.Local_Ratio;
                                                        strsend = strsend + " " + RatioValue.ToString();
                                                    }
                                                }
                                            }
                                            string message = "SEND" + " " + EP[sks.Ip.ToString()].CDSDT[1].Stringvalue + conmmandName + "完成";
                                            //TraceMethodInfo(message);
                                            CreatLog(sks.Ip.Address.ToString(), message);
                                            tcpServer.SendToClient(sks.Ip, PackData(strsend));
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                TraceMethodInfo(ex.Message + ex.StackTrace);
            }
        }
        private void RLAMAN(IPEndPoint IP)
        {
            try
            {
                Raman.数据初始化();
                if (Raman.BWTTcpClientClassCD[0].已连接)
                {
                    Raman.LoginTimer.Enabled = true;
                    Raman.ramancomplete = false;
                    int Times = 0;
                    while (true)
                    {
                        if (Times == 30) { return; }
                        if (Raman.ramancomplete)
                        {
                            Raman.ramancomplete = false;
                            string strsend = "RLAMAN" + " " + Convert.ToInt32(Raman.BWTTcpClientClassCD[0].拉曼差值 * 10);
                            strsend = "RLAMAN" + " " + Convert.ToInt32(880);
                            tcpServer.SendToClient(IP, PackData(strsend));
                            return;
                        }
                        Thread.Sleep(1000);
                        Times++;
                    }
                }
                else
                {
                    string message = "RLAMANS" + " " + "0";
                    CreatLog(IP.Address.ToString(), message);
                }

            }
            catch (Exception ex)
            {
                TraceMethodInfo(ex.Message + ex.StackTrace);
            }
        }
        private void RLAMANS(IPEndPoint IP)
        {
            try
            {
                string strsend = "";
                if (Raman.BWTTcpClientClassCD[0].已连接)
                {
                    strsend = "RLAMANS" + " " + "1";
                }
                else
                {
                    strsend = "RLAMANS" + " " + "0";
                }
                tcpServer.SendToClient(IP, PackData(strsend));
            }
            catch (Exception ex)
            {
                TraceMethodInfo(ex.Message + ex.StackTrace);
            }
        }
        private bool InitializeData(string SN, string IP, string NPRP, Data0 data0,Data1 data1,double powerall)
        {
            //Public.计时起点();
            try
            {
                bool resuiltbool = false;
                string message = "";
                for (int i = 0; i < EP[IP].CDSDT.Count; i++)
                {
                    if (EP[IP].CDSDT[i].Local_NameE != "JobNo")
                    {
                        EP[IP].CDSDT[i].Stringvalue = "N/A";
                    }
                }


                //string PN;
                //string PRTI; //功率电阻型号指令
                //string RLP; //红光功率
                //string PCBSN;//PCB版号
                //string bengyuanSN;//泵源SN
                //NPRP = "2";//功率电阻并联个数
                string NPN = "";
                if (powerall<4325)
                {
                    NPN = "BFL-CW15-B";
                }
                else 
                {
                    NPN = "BFL-CW20-B";
                }
                string[] yunArray = NPN.Split(new string[] { "-" }, StringSplitOptions.RemoveEmptyEntries);
                if (yunArray[1] == "CW05")
                {
                    yunArray[1] = "CW500F";
                }
                else if (yunArray[1] == "CW10")
                {
                    yunArray[1] = "CW1000F";
                }
                else if (yunArray[1] == "CW15")
                {
                    yunArray[1] = "CW1500G";
                }
                else if (yunArray[1] == "CW20")
                {
                    yunArray[1] = "CW2000D";
                }
                else if (yunArray[1] == "CW30")
                {
                    yunArray[1] = "CW3000D";
                }
                string SPN = "";
                for (int i = 0; i < yunArray.Count(); i++)
                {
                    if (i == 0)
                    {
                        SPN = yunArray[0];
                    }
                    else
                    {
                        SPN = SPN + "-" + yunArray[i];
                    }
                }
                string[] yunArray1 = SPN.Split(new string[] { "-" }, StringSplitOptions.RemoveEmptyEntries);
                string input = yunArray1[1].Substring(yunArray1[1].Length - 1, 1);
                bool isNumeric = Regex.IsMatch(input, @"^\d$");
                string power = ""; string category = "";
                if (!isNumeric)
                {
                    power = yunArray1[1].Substring(2, yunArray1[1].Length - 3);
                    category = yunArray1[1].Substring(yunArray1[1].Length - 1, 1);
                }
                else
                {
                    power = yunArray1[1].Substring(2, yunArray1[1].Length - 2);
                }

                string ROPOL = power;///激光器额定输出功率                                           
                string MPC = "";//最大功率电流

                if (category == "D" || category == "F" || category == "G" || category == "P")
                {
                    ROPOL = (int.Parse(ROPOL) * 1067 / 1000).ToString();
                }
                ROPOL = "3000";
                RecondData("PN", data1.SUPER_PRODUCT_TYPE, IP);
                RecondData("SN", SN, IP);
                RecondData("PRTI", data1.resistance_type, IP);
                RecondData("RLP", data1.hg_power, IP);
                RecondData("ROPOL", ROPOL, IP);
                RecondData("NPRP", NPRP, IP);

                //通过PN查找PCB版号，再由功率和版号结合确定REQ文件名
                string REQName = Public.FindREQName(SPN, IP, data1.control_panel_type);
                string REQNamepower = REQName + power;
                EP[IP].PCBSN = data1.control_panel_type;
                //赋值RPV
                string RPV = "";
                if (Public.FindRPV(REQNamepower,data0,data1, out MPC, out RPV, IP))
                {
                    RecondData("MPC", MPC, IP);

                    string[] RPVArray = RPV.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);

                    for (int i = 0; i < EP[IP].CDSDT.Count; i++)
                    {
                        if (EP[IP].CDSDT[i].Local_CommandName == "RPV")
                        {
                            int Index = i - EP[IP].CDSDT[i].Local_CMDID + 1;
                            EP[IP].CDSDT[i].Stringvalue = RPVArray[Index];
                        }
                    }
                }
                else
                {
                    message = SN + ":" + "泵源数据库查询错误！";
                    //TraceMethodInfo(message);
                    CreatLog(IP, message);
                    return resuiltbool;
                }
                bool Is235 = false;
                if (EP[IP].CDSDT[60].Stringvalue == "1")
                {
                    Is235 = true;
                }

                //通过PN查找PCB版号，再由功率和版号结合确定REQ文件名
                EP[IP].REQList = Public.ReadREQtxt(REQName, data1.switch_power_type, Is235, power);
                //通过PN确定WCON并且记录
                string 文件号001 = "";
                文件号001 = Public.CD001文件号[REQName].ToString();
                string WCON = "";
                if (!Public.SeekWCON(SPN, 文件号001, out WCON))
                {
                    return resuiltbool;
                }
                //string WCON = Public.SeekWCON(PN, 文件号001);
                string[] WCONArray = WCON.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < EP[IP].CDSDT.Count; i++)
                {
                    if (EP[IP].CDSDT[i].Local_CommandName == "WCON")
                    {
                        int Index = i - EP[IP].CDSDT[i].Local_CMDID + 1;
                        EP[IP].CDSDT[i].Stringvalue = WCONArray[Index];
                    }
                }

                //判断WDP
                string WDP = "";
                if (power == "3000")
                {
                    WDP = "WDP 1 1 1 0 0 0";
                }
                else
                {
                    WDP = "WDP 1 1 0 0 0 0";
                }
                string[] WDPArray = WDP.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < EP[IP].CDSDT.Count; i++)
                {
                    if (EP[IP].CDSDT[i].Local_CommandName == "WDP")
                    {
                        int Index = i - EP[IP].CDSDT[i].Local_CMDID +1 ;
                        EP[IP].CDSDT[i].Stringvalue = WDPArray[Index];
                    }
                }

                //给PCB记录检测的北京时间
                string[] WATIMEArray = new string[8];
                DateTime DT = Public.ChinaAreaTime(DateTime.Now);
                //记录开始时间
                EP[IP].CDSDT[0].Stringvalue = DT.AddHours(0).ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.CurrentCulture);

                WATIMEArray[0] = "WATIME";
                WATIMEArray[1] = DT.Year.ToString().Substring(2, 2);
                WATIMEArray[2] = DT.Month.ToString();
                WATIMEArray[3] = DT.Day.ToString();
                string strDT = "";
                if (((int)DT.DayOfWeek).ToString() == "0")
                {
                    strDT = "7";
                }
                else
                {
                    strDT = ((int)DT.DayOfWeek).ToString();
                }
                WATIMEArray[4] = strDT;
                WATIMEArray[5] = DT.Hour.ToString();
                WATIMEArray[6] = DT.Minute.ToString();
                WATIMEArray[7] = DT.Second.ToString();
                for (int i = 0; i < EP[IP].CDSDT.Count; i++)
                {
                    if (EP[IP].CDSDT[i].Local_CommandName == "WATIME")
                    {
                        int Index = i - EP[IP].CDSDT[i].Local_CMDID + 1;
                        EP[IP].CDSDT[i].Stringvalue = WATIMEArray[Index];
                        //EP[sks.Ip.ToString()].CDSDT[i].doublevalue = Convert.ToDouble(RPVArray[Index]);
                    }
                }
                resuiltbool = true;
                return resuiltbool;
            }
            catch (Exception ex)
            {

                TraceMethodInfo(ex.Message + ex.StackTrace);
                return false;
            }
            //Public.计时终点();
            //string st = Public.时间差.ToString();
            //Console.WriteLine(Public.时间差.ToString());
        }
        private void TcpConnectEvent(object sender, EventArgs e)
        {
            try
            {
                StartServerToolStripMenuItem();

                if (tcpServer.ClientList.Count > 0)
                {
                    for (int i = 0; i < tcpServer.ClientList.Count; i++)
                    {
                        string host = tcpServer.ClientList[i].Ip.Address.ToString();
                        int port = tcpServer.ClientList[i].Ip.Port;

                        if (!TestConnection(host))
                        {
                            tcpServer.ClientList[i].Client.Close();
                            tcpServer.ClientList.Remove(tcpServer.ClientList[i]);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                TraceMethodInfo(ex.Message + ex.StackTrace);
            }

        }
        #region 采用Socket方式，测试服务器连接
        /// <summary>
        /// 采用Socket方式，测试服务器连接
        /// </summary>
        /// <param name="host">服务器主机名或IP</param>
        /// <param name="port">端口号</param>
        /// <param name="millisecondsTimeout">等待时间：毫秒</param>
        /// <returns></returns>
        public bool TestConnection(string host)
        {
            bool TestResult = false;
            //IsTcpConnectTesting = true;
            Ping ping = new Ping();
            int timeout = 2000;
            PingReply pingReply = ping.Send(host, timeout);

            if (pingReply.Status == IPStatus.Success)
            {
                TestResult = true;
            }
            else
            {
                TestResult = false;
            }
            //IsTcpConnectTesting = false;
            return TestResult;
        }
        #endregion
        public static void RecondData(string DataName, string value, string IP)
        {
            DataRow[] DR = Public.SDT.Select("NameE = '" + DataName + "'");
            int N = Convert.ToInt32(DR[0]["ID"]);
           Public.EP[IP].CDSDT[N].Stringvalue = value;

        }
        public static void ReadData(string DataName, out string value, string IP)
        {
            DataRow[] DR = Public.SDT.Select("NameE = '" + DataName + "'");
            if (DR.Count() > 0)
            {
                int N = Convert.ToInt32(DR[0]["ID"]);
                value =Public.EP[IP].CDSDT[N].Stringvalue;
            }
            else
            {
                value = "N/A";
            }
        }
        public static byte[] PackData(string command)
        {
            try
            {
                PackedData = new List<byte>();
                string tempcommand = command.TrimEnd('\r', '\n');

                PackedData.Clear();
                PackedData.Add(0xEB);
                PackedData.Add(0x90);

                PackedData.AddRange(BitConverter.GetBytes(command.Length + 4));//长度

                PackedData.Add(0x01);
                PackedData.Add(0x00);
                PackedData.AddRange(Encoding.UTF8.GetBytes(command));
                PackedData.Add(0x0D);//CR
                PackedData.Add(0x0A);//LF

                ushort crc16 = CRC16(PackedData.Skip(6).ToArray());
                PackedData.AddRange(BitConverter.GetBytes(crc16));

                byte[] bArrary = PackedData.ToArray();
                return bArrary;
            }
            catch (Exception ex)
            {
                byte[] bArrary = new byte[0];
                return bArrary;
                //_mLog.Error(ex.Message);
            }
        }
        private void MysqlUpdateTimerEvent()
        {
            try
            {
                if (EP.Count > 0)
                {
                    string[] connectListkeys = new string[EP.Count];
                    EP.Keys.CopyTo(connectListkeys, 0);

                    foreach (string item in connectListkeys)
                    {
                        if (EP[item].MysqlCanUpdate == true)//4PD
                        {
                            if (EP[item].CDSDT[2].Stringvalue != "N/A" & EP[item].CDSDT[2].Stringvalue != "")
                            {
                                SQL.MysqlDataUpdate3000(EP[item].Client_IP, EP[item].CDSDT[2].Stringvalue, EP[item].CDSDT[0].Stringvalue);
                                EP[item].MysqlCanUpdate = false;
                                Thread.Sleep(50);
                            }

                        }
                    }
                }
            }
            catch (Exception ex)
            {
                TraceMethodInfo(ex.Message + ex.StackTrace);
            }
        }
        private void MysqlUpdate(string IP)
        {
            try
            {
                if (EP[IP].CDSDT[1].Stringvalue != "N/A" & EP[IP].CDSDT[1].Stringvalue != "")
                {
                    SQL.MysqlDataUpdate3000(EP[IP].Client_IP, EP[IP].CDSDT[1].Stringvalue, EP[IP].CDSDT[0].Stringvalue);
                    EP[IP].MysqlCanUpdate = false;
                    //Thread.Sleep(50);
                }              
            }
            catch (Exception ex)
            {
                TraceMethodInfo(ex.Message + ex.StackTrace);
            }
        }
        private bool IRAYset(object obj)
        {
            try
            {
                string IP = obj as string;

                string strtoken = "";
                string IRAYtype = "class1000";
                string[] IRAYIPList = IP.Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries);

                double IP4 = Convert.ToDouble(IRAYIPList[3]) + 1;
                IRAYIPList[3] = IP4.ToString();
                string IRAYIP = IRAYIPList[0] + "." + IRAYIPList[1] + "." + IRAYIPList[2] + "." + IRAYIPList[3];

                SQL.Mysqlreadinfraredcameraarea();
                if (IRAYClass.GetToken(IRAYIP, out strtoken))
                {
                    for (int i = 0; i < 10; i++)
                    {
                        IRAYClass.DelTemperatureArea(IRAYIP, strtoken, i);
                        Thread.Sleep(5);
                    }
                    for (int i = 0; i < 10; i++)
                    {
                        int startX = Public.CDIRAYClass[IRAYtype].CDIRAYArea[i].startX;
                        int startY = Public.CDIRAYClass[IRAYtype].CDIRAYArea[i].startY;
                        int endX = Public.CDIRAYClass[IRAYtype].CDIRAYArea[i].endX;
                        int endY = Public.CDIRAYClass[IRAYtype].CDIRAYArea[i].endY;
                        IRAYClass.AddTemperatureArea(IRAYIP, strtoken, startX, startY, endX, endY);
                        Thread.Sleep(5);
                    }
                }

                return true;
            }
            catch (Exception ex)
            {
                string IP = obj as string;

                TraceMethodInfo(ex.Message + ex.StackTrace);
                return false;
            }

        }
        static async Task test()
        {
            Task[] tasks = new Task[3];
            tasks[0] = wait();
            tasks[1] = wait2();
            tasks[2] = wait3();

            await Task.WhenAll(tasks);

            Console.WriteLine("main");
            Console.ReadKey();

        }
        public static async Task wait()
        {
            await Task.Delay(2000);
            Console.WriteLine("Wait 2000 ms");
        }
        public static async Task wait2()
        {
            await Task.Delay(3000);
            Console.WriteLine("Wait 3000 ms");
        }
        public static async Task wait3()
        {
            await Task.Delay(4000);
            Console.WriteLine("Wait 4000 ms");
        }
        public static void CreatLog(string IP, string message)
        {

            string day = DateTime.Now.ToString("d");
            string dic = Directory.GetCurrentDirectory() + "\\" + "Logs" + "\\" + day;
            if (!Directory.Exists(dic))
            {
                Directory.CreateDirectory(dic);
            }

            string filename = dic + "\\" + IP + ".txt";
            try
            {
                FileStream fs = new FileStream(filename, FileMode.Append, FileAccess.Write);
                using (StreamWriter sw = new StreamWriter(fs))
                {
                    string time = DateTime.Now.ToString("T") + "." + DateTime.Now.ToString("fff");
                    string str = time + "-内容-" + message;
                    sw.WriteLine(str);
                }
                fs.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
